package win.demonlegion.common;

import com.lmax.disruptor.dsl.Disruptor;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import win.demonlegion.disruptor.factory.LongEventFactory;
import win.demonlegion.disruptor.handler.LongEventHandler;
import win.demonlegion.disruptor.module.LongEvent;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

@Component
public class DisruptorDemo {

    @Bean
    public Disruptor<LongEvent> getDemoDisruptor() {
        Executor executor = Executors.newCachedThreadPool();
        int bufferSize = 1024;
        Disruptor<LongEvent> disruptor = new Disruptor<>(new LongEventFactory(), bufferSize, executor);        // Connect the handler
        disruptor.handleEventsWith(new LongEventHandler());
        disruptor.start();
        return disruptor;
    }
}
